﻿using System.Linq;

namespace System.Collections.Generic
{
    public static class IDictionaryExtension
    {
        #region 判定字典是否为null
        /// <summary>
        /// 判定字典是否为null
        /// </summary>
        /// <param name="currentDictionary">当前字典</param>
        /// <returns>判定结果</returns>
        public static bool IsNull<TKey, TValue>(this IDictionary<TKey, TValue> currentDictionary)
        {
            return currentDictionary == null;
        }
        #endregion

        #region 设定值

        /// <summary>
        /// 设定值

        /// </summary>
        /// <param name="key">参数key</param>
        /// <param name="currentDictionary">参数值</param>
        /// <param name="value">当前字典</param>
        public static void SetValue<TKey, TValue>(this IDictionary<TKey, TValue> currentDictionary, TKey key, TValue value)
        {
            if (currentDictionary.ContainsKey(key))
            {
                currentDictionary[key] = value;
            }
            else
            {
                currentDictionary.Add(key, value);
            }
        }
        #endregion

        #region 获取传递参数

        /// <summary>
        /// 获取传递参数

        /// </summary>
        /// <param name="key">参数key</param>
        /// <param name="currentDictionary">当前字典</param>
        /// <param name="removeKey">是否移除</param>
        /// <returns>返回值</returns>
        public static TValue GetValue<TKey, TValue>(this IDictionary<TKey, TValue> currentDictionary, TKey key, bool removeKey = false)
        {
            TValue queryParameter = default(TValue);
            if (!currentDictionary.IsNull())
            {
                currentDictionary.TryGetValue(key, out queryParameter);

                if (removeKey)
                {
                    currentDictionary.RemoveByKey(key);
                }
            }
            return queryParameter;
        }
        #endregion

        #region 移除参数
        /// <summary>
        /// 移除参数
        /// </summary>
        /// <param name="key">key值</param>
        /// <param name="currentDictionary">当前字典</param>
        public static void RemoveByKey<TKey, TValue>(this IDictionary<TKey, TValue> currentDictionary, TKey key)
        {
            if (!currentDictionary.IsNull())
            {
                if (currentDictionary.ContainsKey(key))
                {
                    currentDictionary.Remove(key);
                }
            }
        }
        #endregion

        #region 通过Key列表移除参数
        /// <summary>
        /// 通过Key列表移除参数
        /// </summary>
        /// <typeparam name="TKey">Key类型</typeparam>
        /// <typeparam name="TValue">值类型</typeparam>
        /// <param name="currentDictionary">当前字典</param>
        /// <param name="keys">key值</param>
        public static void RemoveByKeys<TKey, TValue>(this IDictionary<TKey, TValue> currentDictionary,params TKey[] keys)
        {
            if (!currentDictionary.IsNull())
            {
                var removeKeys = currentDictionary.Keys.Where(item => keys.Contains(item));
                
                foreach (var item in removeKeys)
                {
                    if (currentDictionary.ContainsKey(item))
                    {
                        currentDictionary.Remove(item);
                    }
                }
            }
        }
        #endregion
    }
}
